const apiKey = 'sk-79c3960e4ae24cc496cc249ecad9ac64';

class DeepSeekChat {
    constructor() {
        this.init();
        this.bindEvents();
    }

    init() {
        // 创建聊天对话框
        const dialogHTML = `
            <div class="deepseek-dialog">
                <div class="dialog-header">DeepSeek Chat</div>
                <div class="chat-container" id="chatContainer"></div>
                <div class="input-container">
                    <input type="text" class="chat-input" id="chatInput" placeholder="输入消息...">
                    <button class="send-btn" id="sendBtn">发送</button>
                </div>
            </div>
        `;
        document.body.insertAdjacentHTML('beforeend', dialogHTML);

        // 获取DOM元素
        this.deepseekBtn = document.querySelector('.deepseek-btn');
        this.dialog = document.querySelector('.deepseek-dialog');
        this.chatContainer = document.getElementById('chatContainer');
        this.chatInput = document.getElementById('chatInput');
        this.sendBtn = document.getElementById('sendBtn');
    }

    bindEvents() {
        // 切换对话框显示状态
        this.deepseekBtn.addEventListener('click', (e) => {
            e.stopPropagation();
            this.toggleDialog();
        });

        // 点击其他地方关闭对话框
        document.addEventListener('click', (e) => {
            if (!this.dialog.contains(e.target) && !this.deepseekBtn.contains(e.target)) {
                this.dialog.style.display = 'none';
            }
        });

        // 发送按钮点击事件
        this.sendBtn.addEventListener('click', () => this.sendMessage());

        // 按回车发送消息
        this.chatInput.addEventListener('keypress', (e) => {
            if (e.key === 'Enter') {
                this.sendMessage();
            }
        });
    }

    toggleDialog() {
        this.dialog.style.display = this.dialog.style.display === 'none' ? 'block' : 'none';
    }

    async sendMessage() {
        const message = this.chatInput.value.trim();
        if (!message) return;

        // 显示用户消息
        this.appendMessage(message, true);
        this.chatInput.value = '';

        try {
            const response = await fetch('https://api.deepseek.com/v1/chat/completions', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'Authorization': `Bearer ${apiKey}`
                },
                body: JSON.stringify({
                    model: "deepseek-chat",
                    messages: [{
                        role: "user",
                        content: message
                    }]
                })
            });

            const data = await response.json();
            const botResponse = data.choices[0].message.content;
            this.appendMessage(botResponse, false);
        } catch (error) {
            console.error('Error:', error);
            this.appendMessage('抱歉，发生错误，请稍后重试。', false);
        }
    }

    appendMessage(message, isUser) {
        const messageDiv = document.createElement('div');
        messageDiv.className = `message ${isUser ? 'user-message' : 'bot-message'}`;
        messageDiv.textContent = message;
        this.chatContainer.appendChild(messageDiv);
        this.chatContainer.scrollTop = this.chatContainer.scrollHeight;
    }
} 